package cn.iocoder.yudao.module.dt.controller.admin.feeitem;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.dt.controller.admin.feeitem.vo.*;
import cn.iocoder.yudao.module.dt.dal.dataobject.feeitem.FeeItemDO;
import cn.iocoder.yudao.module.dt.convert.feeitem.FeeItemConvert;
import cn.iocoder.yudao.module.dt.service.feeitem.FeeItemService;

@Api(tags = "管理后台 - 活动价格")
@RestController
@RequestMapping("/dt/fee-item")
@Validated
public class FeeItemController {

    @Resource
    private FeeItemService feeItemService;

    @PostMapping("/create")
    @ApiOperation("创建活动价格")
    @PreAuthorize("@ss.hasPermission('dt:fee-item:create')")
    public CommonResult<Integer> createFeeItem(@Valid @RequestBody FeeItemCreateReqVO createReqVO) {
        return success(feeItemService.createFeeItem(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新活动价格")
    @PreAuthorize("@ss.hasPermission('dt:fee-item:update')")
    public CommonResult<Boolean> updateFeeItem(@Valid @RequestBody FeeItemUpdateReqVO updateReqVO) {
        feeItemService.updateFeeItem(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除活动价格")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('dt:fee-item:delete')")
    public CommonResult<Boolean> deleteFeeItem(@RequestParam("id") Integer id) {
        feeItemService.deleteFeeItem(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得活动价格")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('dt:fee-item:query')")
    public CommonResult<FeeItemRespVO> getFeeItem(@RequestParam("id") Integer id) {
        FeeItemDO feeItem = feeItemService.getFeeItem(id);
        return success(FeeItemConvert.INSTANCE.convert(feeItem));
    }

    @GetMapping("/list")
    @ApiOperation("获得活动价格列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('dt:fee-item:query')")
    public CommonResult<List<FeeItemRespVO>> getFeeItemList(@RequestParam("ids") Collection<Integer> ids) {
        List<FeeItemDO> list = feeItemService.getFeeItemList(ids);
        return success(FeeItemConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得活动价格分页")
    @PreAuthorize("@ss.hasPermission('dt:fee-item:query')")
    public CommonResult<PageResult<FeeItemRespVO>> getFeeItemPage(@Valid FeeItemPageReqVO pageVO) {
        PageResult<FeeItemDO> pageResult = feeItemService.getFeeItemPage(pageVO);
        return success(FeeItemConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出活动价格 Excel")
    @PreAuthorize("@ss.hasPermission('dt:fee-item:export')")
    @OperateLog(type = EXPORT)
    public void exportFeeItemExcel(@Valid FeeItemExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<FeeItemDO> list = feeItemService.getFeeItemList(exportReqVO);
        // 导出 Excel
        List<FeeItemExcelVO> datas = FeeItemConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "活动价格.xls", "数据", FeeItemExcelVO.class, datas);
    }

}
